$(function(){
	//ajax全局设置
	$.ajaxSetup({
		 beforeSend: function(request) {
            request.setRequestHeader("X-AIRPORT-TOKEN", localStorage.getItem("oauth_token"));
        }
	})
	$('.table-sort').dataTable(pageStting());
	//返回
	$("#returnBackList").on('click',function(){
		window.location.href=baseURL+"/api/special/page/add?ouath_token="+localStorage.getItem("oauth_token");
	})
})
$(window).resize(function(){
	$(".table-sort").css('width','100%');
});
//从服务器分批取数据
pageStting = function(){
		var optsortable = {
					"bPaginate":true,
					"stateSave":true,
					"bProcessing":true,
					"iDisplayLength":10,
					"bLengthChange":false,
					"sAjaxSource":baseURL+'/api/special/template/list?referer='+Math.random(),
					"sPaginationType":'full_numbers',
					"bServerSide": true,
					"bSort": false,             
					"bFilter": false,  
				     "aoColumnDefs": [                 
			                          	   {               
			                          		   "bVisible":false,
			                          		   "aTargets":[0]
				                           },
				                           /*{               
					                            "render": function (data, type, full){  
					                            		var edit_herf ="<span class='text-c c-blue handlePointer' title='发布特情' onclick=specialAddShow("+full[0]+")>"+full[1]+"</span>"
					        							return edit_herf;
					                              },
					                            "targets":1 
					                           },*/
				                          {               
					                            "render": function (data, type, full){  
					                            		var edit_herf ="<div class='text-c handlePointer width80px'><img title='修改' alt='修改' src='"+baseURL+"/images/admin/cp_edite.png' onclick=specialTempShow('"+full[0]+"') />"
					        							var trach_herf="<img title='删除' alt='删除' class='ml-20' src='"+baseURL+"/images/admin/cp_delete.png' onclick=\"return deleteData('"+full[0]+"')\" /></div>"
					        							return edit_herf + trach_herf;
					                              },
					                            "targets":3 
					                           }
				                        ] ,
					"fnServerData": function(sSource, aoData, fnCallback){
							var globalSearch = {};
							var searchConditions = {};
							if($.trim($("#searchTemplet").val())!=''){
								searchConditions.searchTopic=$.trim($("#searchTemplet").val());
							}
							globalSearch.name= 'globalSearch';
							globalSearch.value= JSON.stringify(searchConditions);
							aoData.push(globalSearch);
							 $.ajax({  
						        type: "GET",     
						        url: sSource,     
						        dataType: "json",    
						        contentType: "application/json;charset=UTF-8", 
						        data: aoData, // 以json格式传递  
						        success: function(result) {
						        	if(!result.flag){
					            		  layer.msg("查询失败！");
					            		  return;
					            	}
						        	if(result.flag){
							        	var obj =result;
										var data = obj.results;
										var aaData = [];
										var dataResult = {};
										dataResult.iTotalRecords = obj.totalcount;
										dataResult.iTotalDisplayRecords= obj.totalcount;
										dataResult.iDisplayStart = obj.offset;
										dataResult.iDisplayLength= obj.limit;
										dataResult.aaData = aaData;
										//json重构数组对象 
										$.each(data,function(i,val){
											aaData[i]=[]
											aaData[i].push(val.id);
											aaData[i].push(getContent(val.topic));
											aaData[i].push(getContent(val.content));
											aaData[i].push(val.null_n);
										});
							            fnCallback(dataResult);   
						          }
						        }    
						     });
					}
				}	
		return optsortable;
}

function doSeacrh(){
	   var seachValue={};
	   $('.table-sort').DataTable().search(seachValue).draw();
}
function addTempletPage(){
	window.location.href=baseURL+"/api/special/template/page/add?ouath_token="+localStorage.getItem("oauth_token");
}
specialAddShow=function(id){
	window.location.href=baseURL+"/api/special/page/add?tempId="+id+"&ouath_token="+localStorage.getItem("oauth_token");
}
specialTempShow=function(id){ 
	var times= new Date().getTime();
	window.location.href=baseURL+"/api/special/template/page/edit/"+id+"?times="+times+"&ouath_token="+localStorage.getItem("oauth_token");
}
deleteData = function(id){
	var data = {};
	data.ids=id;
	layer.confirm('您确定要删除吗？',function(){
		$.ajax({  
	        type: "post",     
	        url: baseURL+'/api/special/template/delete',     
	        dataType: "json",   
	        contentType: "application/json",
	        data: JSON.stringify(data),
	        success: function(result) {
	        	   if(!result.flag){
	        		   layer.msg("删除失败！原因："+result.message);
	        		   return;
	        	   }else{
	        		   location.reload();
	        	   }
	        }    
	     });
	})
}
function getContent(obj){
	if(obj.length<=80){
		return "<span>"+obj+"</span>";
	}else return "<span>"+obj.substring(0,80)+"...</span>";
}